<?php

namespace zhtfrontend\models\datas;

use common\components\helpers\Fun;
use common\models\datas\Setting;
use Yii;

use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use common\components\extenders\MenuManage;
use yii\helpers\Html;
use zhtfrontend\models\forms\TeacherSearchForm;

/**
 * This is the model class for table "{{%arrangement}}".
 *
 * @property integer $id
 * @property integer $course_id
 * @property integer $teacher_id
 * @property integer $classroom_id
 * @property integer $classroom_attr_id
 * @property integer $class_length
 * @property integer $class_count
 * @property integer $max_number
 * @property integer $opening_number
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $is_booking
 * @property integer $booking_count
 * @property string $booking_price
 * @property integer $is_online
 * @property integer $room_type
 */
class Arrangement extends \zhtfrontend\components\bases\BaseModel
{
    public $mode; //模式
//    public $oldprice;
//    public $oldcount;
    public $max_age;
    public $min_age;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%arrangement}}';
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            //[['admin_id','course_id', 'teacher_id', 'classroom_id',  'class_length', 'class_count', 'max_number', 'opening_number', 'created_at', 'updated_at', 'is_booking', 'booking_count','is_delete','oldcount','max_age','min_age'], 'integer'],
            [['admin_id','course_id', 'classroom_id',  'class_length', 'class_count', 'max_number', 'opening_number', 'created_at', 'updated_at', 'is_booking', 'booking_count','is_delete','max_age','min_age','teaching_methods','is_group','category_id','tutor_id','mechanic_id','room_type','class_hour'], 'integer'],
            [['course_id', 'teacher_id', 'classroom_id', 'max_number', 'opening_number', 'classroom_attr_id', 'is_booking','is_online','max_age','min_age','is_group'], 'required', 'on' => ['create','update']],
            //[['booking_price','price','market_price','cost','oldprice'], 'number'],
            [['booking_price','price','market_price','cost'], 'number'],
//            [['price','class_count'],'required','when'=>function(){
//                return $this->is_online == 1;
//            }],
//            [['oldprice','oldcount'],'required','when'=>function(){
//                return $this->is_online == 0;
//            }],
            [['suitable_age'],'string','max'=>100],
            [['age_group'],'string','max'=>100],
            [['classroom_attr_id','teacher_id'],'string','max'=>150],
           // [['classroom_attr_id'],'check_tadylesson','on'=>['create','update']],
            //[['max_number'],'check_effective','on'=>['create','update']],
            ['max_number', 'compare', 'compareAttribute' => 'opening_number', 'operator' => '>=','message'=>'最满人数的值必须大于或等于开班人数'],
            [['opening_number'],'check_opening','on'=>['create','update']],
            [['max_age'],'check_suitable_age','on'=>['create','update']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增编号'),
            'admin_id'=>Yii::t('zhtfrontend','管理员id'),
            'course_id' => Yii::t('zhtfrontend', '课程id'),
            'teacher_id' => Yii::t('zhtfrontend', '老师id'),
            'classroom_id' => Yii::t('zhtfrontend', '教室id'),
            'classroom_attr_id' => Yii::t('zhtfrontend', '上课时间'),
            'class_length' => Yii::t('zhtfrontend', '上课时长'),
            'class_count' => Yii::t('zhtfrontend', '上课次数'),
            'max_number' => Yii::t('zhtfrontend', '最满人数'),
            'opening_number' => Yii::t('zhtfrontend', '开班人数'),
            'is_delete' => Yii::t('zhtfrontend', '是否删除'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'updated_at' => Yii::t('zhtfrontend', '修改时间'),
            'is_booking' => Yii::t('zhtfrontend', '是否可预约'),
            'booking_count' => Yii::t('zhtfrontend', '可预约课次'),
            'booking_price' => Yii::t('zhtfrontend', '预约价格'),
            'price'=>Yii::t('zhtfrontend', '单节课次价格'),
            'market_price'=>Yii::t('zhtfrontend', '市场价格'),
            'cost'=>Yii::t('zhtfrontend', '成本'),
            'is_online'=>Yii::t('zhtfrontend', '课程类型'),
            'mode' => Yii::t('zhtfrontend','模式'),
//            'oldcount' => Yii::t('zhtfrontend','上课次数'),
//            'oldprice' => Yii::t('zhtfrontend','价格'),
            'suitable_age' => Yii::t('zhtfrontend','适合年龄段'),
            'max_age' => Yii::t('zhtfrontend','最大年龄'),
            'min_age' => Yii::t('zhtfrontend','最小年龄'),
            'is_group' => Yii::t('zhtfrontend','是否是艺教学'),
            'teaching_methods' => Yii::t('zhtfrontend','课程方式'),
            'category_id' => Yii::t('zhtfrontend','三大类'),
            'tutor_id' => Yii::t('zhtfrontend','选择导师'),
            'mechanic_id' => Yii::t('zhtfrontend','机构id'),
            'age_group' => Yii::t('zhtfrontend','年龄段'),
            'room_type' => Yii::t('zhtfrontend','教室组类型'),
            'class_hour' => Yii::t('zhtfrontend','课时数'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['is_delete'] = function () {
            switch ($this->is_delete) {
                case 0:
                    return \Yii::t('zhtfrontend', '未删除');
                case 1:
                    return \Yii::t('zhtfrontend', '已删除');
            }
        };
        $fields['is_online'] = function () {
            switch ($this->is_online) {
                case 0:
                    return \Yii::t('zhtfrontend', '老生班');
                case 1:
                    return \Yii::t('zhtfrontend', '新生班');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return isset($this->admin) ? $this->admin->username : '';
            },
            'course_name' => function () {
                return isset($this->course) ? $this->course->name : '';
            },
            'teacher_name' => function () {
                $string='';
                if(!empty($this->teacher_id)) {
                    $teacher_ids = explode(',', $this->teacher_id);
                    $teacherlist = Teacher::find()->select('name')->where(['in', 'id', $teacher_ids])->column();
                    $string = implode(',', $teacherlist);
                }
                return $string;
            },
            'teacher_form' => function () {
                return ($this->course) != null ? $this->course->teache_form : '';
            },
            'booking' => function(){
                switch ($this->is_booking) {
                    case 0:
                        return \Yii::t('zhtfrontend', '否');
                    case 1:
                        return \Yii::t('zhtfrontend', '是');
                }
            },
            'online' => function(){
                switch ($this->is_online) {
                    case 0:
                        return \Yii::t('zhtfrontend', '否');
                    case 1:
                        return \Yii::t('zhtfrontend', '是');
                }
            },
            'class_time'=>function(){
                $string='';
                $classroom_attr_ids=explode(',',$this->classroom_attr_id);
                if(!empty($classroom_attr_ids)){
                    $classroom_attr=ClassroomAttr::find()->where(['in','id',$classroom_attr_ids])->all(Fun::getSiteDb());
                    if($classroom_attr!==null){
                        foreach($classroom_attr as $k=>$v){
                            $class_time=Fun::getClassTime($v->start_time,$v->end_time,$v->classday);
                            $string==''?$string=$class_time:$string.='|'.$class_time;
                        }
                    }
                }
                return $string;
            },
            'creategoods'=>function(){
                if(empty($this->goods)){
                    return \Yii::t('zhtfrontend', "<button type='button' style='padding:0;color:#fe6666;background: none;border:0' title='点击生成商品' class='shelve' data-id='".$this->id."'>生成商品</button>");
                }else{
                    if($this->updated_at != $this->goods->updated_at){
                            return \Yii::t('zhtfrontend', "<button type='button' style='padding:0;color:#638dc5;background: none; border:0' title='点击更新商品' class='shelve' data-id='".$this->id."'>更新商品</button>");
                        }else{
                            return \Yii::t('zhtfrontend','最新商品');
                        }
                }
            },
            'room_number' => function () {
                return isset($this->classroom)?$this->classroom->room_number:'';
            },
            'room_name' => function () {
                return isset($this->classroom)?$this->classroom->name:'';
            },
            'group_room_name' => function(){
                return isset($this->classroomgroup)?$this->classroomgroup->name:'';
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    ['delete' => function ($data) {
                            $state = ClassManage::findAll(['associate_id' => $data['id'],'is_old'=>1]);
                            if (!empty($state)) {
                                return '<button class="yx-btn-disabled" disabled>删除</button>';
                            } else {
                                return '';
                            }
                        },
                        'update' => function ($data) {
                                return '';
                        },

                    ]
                )->create();
            },
        ];
    }


    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'admin_id']);
    }

    public function  getCourse()
    {
        return $this->hasOne(Course::className(), ['id' => 'course_id']);
    }

    public function  getTeacher()
    {
        return $this->hasOne(Teacher::className(), ['id' => 'teacher_id']);
    }

    public function  getClassroom()
    {
        return $this->hasOne(Classroom::className(), ['id' => 'classroom_id']);
    }

    public function  getGoods()
    {
        return $this->hasOne(Goods::className(), ['arrangement_id' => 'id']);
    }

    public function getClassattr(){
        return $this->hasOne(ClassManageAttribute::className(),['arrangement_id'=>'id']);
    }
    public function getClassroomgroup(){
        return $this->hasOne(ClassroomGroup::className(),['id'=>'classroom_id']);
    }

    public function check_tadylesson($attribute){
            if($this->classroom_attr_id != null){
                $ids = explode(',',$this->classroom_attr_id);
                $classday = ClassroomAttr::find()->select('classday')->where(['id'=>$ids])->column();
                $number = array_count_values($classday);
                $max = 0;
                if($this->class_count > 0) {
                    $max = floor($this->class_count / 3);
                }
                foreach ($number as $v) {
                    if ($v > $max) {
                        $this->addError($attribute, Yii::t('zhtfrontend','一天的上课次数不能大于' . $max . '节课'));
                        break;
                    }
                }
            }
    }

//    public function check_effective($attribute){
//        if($this->is_online == 1){
//            $coures = Course::findOne($this->course_id);
//            $goods_data = Goods::find()->where(['mechanic_id'=>$coures->mechince_id])->all();
//            $count = 0;
//            if($goods_data != null) {
//                foreach ($goods_data as $k => $v) {
//                    $goods_attr = GoodsAttr::find()->where(['goods_id' => $v->id, 'obj_type' => 'max_number'])->one();
//                    if ($goods_attr != null)
//                        $count += $goods_attr->value;
//                }
//            }
//            $mechanic = Mechanic::findOne($coures->mechince_id);
//            if(($count + $this->max_number) > $mechanic->enrollment){
//                $this->addError($attribute, Yii::t('zhtfrontend', '招生人数超过机构最大招生数'));
//            }
//        }
//    }
    public function check_opening($attribute){
        if($this->$attribute == 0){
            $this->addError($attribute, Yii::t('zhtfrontend', '开班人数大于0'));
        }
    }

    public function check_suitable_age($attribute){
        if($this->$attribute < $this->min_age){
            $this->addError($attribute, Yii::t('zhtfrontend', '最大年龄不能小于最小年龄'));
        }

    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}
